package com.aaa.web.service.impl.carhouseserviceimpl;

import com.aaa.entity.BackEntrepotRecord;
import com.aaa.entity.BackSchool;
import com.aaa.entity.repair.BackEntrepot;
import com.aaa.phone.systemconst.ISystemConst;
import com.aaa.util.EmpUtil;
import com.aaa.web.commons.NewCarQuery;
import com.aaa.web.commons.WlCommonsCode;
import com.aaa.web.dao.icarhousedao.ICarHouseDao;
import com.aaa.web.service.icarhouseservice.ICarHouseService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ${PROJECT_NAME}
 * @description: 获取所有的新车车辆
 * @author: 王璐
 * @create: 20:07 2018/12/21
 **/
@Service
@Transactional
public class CarHouseServiceImpl implements ICarHouseService,ISystemConst {
    /**
     * 依赖注入
     */
    @Autowired
    private ICarHouseDao dao;

    /**
     * 获取所有的新车车辆
     *
     * @param query
     * @return
     */
    @Override
    public Map<String, Object> getAllCar(NewCarQuery query) {
        //调用公用代码，返回学校id
        int schoolId = WlCommonsCode.isAdmin();
        int page = query.getPage();
        int size = query.getLimit();
        PageHelper.startPage(page, size);
        Map<String, Object> m = new HashMap<>();
        List<Map> allCar = dao.getAllCar(schoolId);
        Integer carCount = dao.getCarCount(schoolId);
        //数据
        m.put("data", allCar);
        //数据总量
        m.put("count", carCount);
        //是否是超管  isAdminSchoolId为0 是超管
        m.put("isAdmin", schoolId);
        //数据总量
        m.put("schoolId", EmpUtil.getEmp().getSchoolid());
        m.put("code", ZERO);
        return m;
    }
    /**
     * 查询新车仓库的所有学校
     * @return
     */
    @Override
    public List<BackSchool> selectSchoolList(){
        return dao.selectSchoolList();
    }
    /**
     * 查询新车车辆
     * @param query
     * @return
     */
    @Override
    public Map<String, Object> selectCar(NewCarQuery query, BackEntrepot backEntrepot) {
        int size = query.getLimit();
        int page = query.getPage();
        PageHelper.startPage(page, size);
        int schoolId = WlCommonsCode.getSchoolId(backEntrepot);
        Map<String, Object> map = new HashMap<>();
        Map<Object, Object> paramMap = new HashMap<>();
        paramMap.put("schoolId",schoolId);
        paramMap.put("goodsName",backEntrepot.getGoodsname());
        List<Map> allCar = dao.selectCar(paramMap);
        Integer carCount = dao.getSelectCarCount(paramMap);

        //数据总量
        map.put("count", carCount);
        //数据
        map.put("data", allCar);
        //是否是超管  0 是超管
        map.put("isAdmin", WlCommonsCode.isAdmin());
        //schoolid
        map.put("schoolId", EmpUtil.getEmp().getSchoolid());
        map.put("code", ZERO);
        return map;
    }

    /**
     * 添加车辆
     * @param backEntrepot
     * @return
     */
    @Override
    public int addCar(BackEntrepot backEntrepot) {
        //在session中获取员工的店名ID
        int schoolId = WlCommonsCode.isAdmin();
        backEntrepot.setSchoolId(schoolId);
        String goodsName = backEntrepot.getGoodsname();
        String operator = EmpUtil.getEmp().getName();
        int exist = dao.getTypeExist(goodsName, schoolId);
        if (exist > ZERO) {
            //如果电动车名已存在，返回204
            return GOODSNAMEEXIST;
        } else {
            //如果电动车名不存在
            //添加数据
            int add = dao.addCar(backEntrepot);
            Map<Object, Object> map = new HashMap<>();
            map.put("backEntrepot",backEntrepot);
            map.put("operator",operator);
            int addRecord = dao.addCarRecord(map);
            //返回添加成功或失败数据
            return WlCommonsCode.result(add,addRecord);
        }

    }

    /**
     * 补货
     *
     * @param goodsName
     * @param goodsQuantity
     * @return
     */
    @Override
    public int addOneTypeCar(String goodsName, int goodsQuantity,double goodsCost) {
        //在session中获取员工的店名ID
        int schoolId = WlCommonsCode.isAdmin();
        //在仓库表添加数据
        Map<Object, Object> map = new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("goodsQuantity",goodsQuantity);
        map.put("schoolId",schoolId);
        int addOne = dao.addOneTypeCar(map);
        //在仓库操作日志表添加数据
        String operator = EmpUtil.getEmp().getName();
        BackEntrepotRecord backEntrepotRecord = new BackEntrepotRecord(goodsName, goodsCost, goodsQuantity, operator, schoolId);
        int addRecord = dao.addOneTypeCarRecord(backEntrepotRecord);
        //返回添加成功或失败数据
        int result = WlCommonsCode.result(addOne,addRecord);
        return result;
    }
    /**
     * 编辑
     * @param goodsName
     * @return
     */
    @Override
    public int editOneTypeCar(String goodsName,String oldGoodsName){
        int schoolId = WlCommonsCode.isAdmin();
        Map<Object, Object> map = new HashMap<>();
        map.put("goodsName",goodsName);
        map.put("oldGoodsName",oldGoodsName);
        map.put("schoolId",schoolId);
        int isUpdate = dao.editOneTypeCarAtRecord(map);
        int update = dao.editOneTypeCar(map);
        return WlCommonsCode.result(isUpdate,update);
    }
}
